import universalBehavior from '../../../behavior/universalBehavior'
import { register } from "../../../service/user";
import { useRequest } from "../../../utils/request";

const WeValidator = require('../../../package/we-validator.min')

Component({
    behaviors: [universalBehavior],
    properties: {},
    data: {
        phone: '',
        second: 0,
        checked: true,
    },
    methods: {
        onLoad() {
            this.getCode = new useRequest('api/phonecode', {
                hold: true,
                loading: 'codeLoading'
            }, this)

            this.validatorInstance = new WeValidator({
                rules: {
                    phone: {
                        required: true,
                        mobile: true
                    },
                    sms_code: {
                        required: true,
                    }
                },
                messages: {
                    phone: {
                        required: '请输入手机号',
                        mobile: '请输入正确的手机号'
                    },
                    sms_code: {
                        required: '请输入验证码',
                    }
                },
            })
        },
        onChange() {
            this.setData({
                checked: !this.data.checked,
            });
        },
        startTimeout() {
            this.setData({
                second: 59
            })
            this.timeout = setInterval(() => {
                this.setData({
                    second: this.data.second - 1
                })
                if (this.data.second <= 0) {
                    clearInterval(this.timeout)
                }
            }, 1000)
        },

        getPhoneCode() {
            if (!WeValidator.checkValue('required', this.data.phone)) {
                wx.showToast({
                    title: '请输入手机号',
                    icon: 'none'
                })
                return false
            }

            if (!WeValidator.checkValue('mobile', this.data.phone, true)) {
                wx.showToast({
                    title: '请输入正确的手机号',
                    icon: 'none'
                })
                return false
            }

            this.getCode.run({ phone: this.data.phone }).then(() => {
                this.startTimeout()
            })
        },
        submit(e) {
            const value = e.detail.value

            if (!this.data.checked) {
                wx.showToast({
                    title: '请先阅读并同意《用户协议》&《隐私声明》',
                    icon: 'none'
                })
                return
            }

            if (this.validatorInstance.checkData(value)) {
                wx.showLoading({
                    title: '正在注册中',
                    mask: true
                })
                register(value).then(() => {
                    wx.hideLoading()
                    wx.showToast({
                        title: '注册成功，即将跳转到首页',
                        icon: 'none',
                        success() {
                            wx.switchTab({
                                url: '/pages/home/index/index'
                            })
                        }
                    })
                })
            }
        },
    }
})
